<template>
  <main>
    <div class="logo">
      <img src="~assets/images/shop_logo.jpg" alt="" class="logo_img">
    </div>
    <van-field
      class="login-input"
      v-model="phone"
      placeholder="请输入手机号码"
      maxlength="11"
      clearable
    />
    <van-field
      v-if="isSms"
      class="login-input"
      v-model="captcha"
      type="digit"
      maxlength="4"
      center
      clearable
      placeholder="请输入短信验证码"
    >
      <template #button v-if="isSms">
        <span class="send-sms disabled" v-if="isSend">{{ count + '秒后重新发送' }}</span>
        <span class="send-sms" v-else-if="smsLoading">发送中...</span>
        <span class="send-sms" @click="sendSms" v-else>发送短信</span>
      </template>
    </van-field>
    <van-field
      v-else
      class="login-input"
      v-model="password"
      type="password"
      placeholder="请输入密码"
      clearable
      maxlength="32"
    />
    <div class="type-link">
      <span @click="loginType">{{ isSms ? '密码登录' : '短信登录' }}</span>
      <router-link to="forgetPassword">忘记密码</router-link>
    </div>
    <van-button class="default-btn" block round @click="login">
      登录
    </van-button>
    <van-divider class="hr">第三方登录</van-divider>
    <van-button type="primary" @click="handwxLogin()" block round>
      <i class="iconfont icon-gongzhonghao"></i>
      微信授权登录
    </van-button>
  </main>
</template>

<script>
import { Dialog, Toast } from 'vant';
import { mapActions } from 'vuex';
import * as Api from 'api/index'

export default {
  name: 'home-view',
  data() {
    return {
      timer: '',
      active: 'home',
      phone: '',
      captcha: '',
      password: '',
      isSms: true,
      countText: '发送短信',
      count: 60,
      isSend: false,
      smsLoading: false
    };
  },
  created() {
    this.$emit('showTabMenu', false);
  },
  methods: {
    ...mapActions([
      'UnSetPassword',
      'Token',
      'GetUserInfo',
      'SetUserInfo',
    ]),
    loginType() {
      this.isSms = !this.isSms
    },
    // 发送短信
    sendSms() {
      if (this.phone.length<11) {
        Toast('请输入正确的手机号！')
        return
      }
      this.smsLoading = true
      Api.SendSms({phone: this.phone}).then(res => {
        Toast(res.message)
        this.isSend = true
        this.smsLoading = false
        this.intervalCount()
      }).catch(() => {
        this.smsLoading = false
      })
    },
    // 倒计时
    intervalCount() {
      this.count = 60
      this.timer = setInterval(() => {
        this.count --
        if (this.count <= 0) {
          this.isSend = false
          clearInterval(this.timer)
        }
      }, 1000)
    },
    // 登录或注册
    login () {
      if (this.phone.length < 11) {
        Toast('请输入正确的手机号!')
        return
      }
      let params = {
        phone: this.phone,
        type: this.isSms ? 'captcha' : 'password'
      }
      if (this.isSms) {
        if (this.captcha.length < 4) {
          Toast('请输入正确的验证码！')
          return
        }
        params.captcha = this.captcha
      } else {
        if (this.password.length < 6) {
          Toast('请输入正确的密码！')
          return
        }
        params.password = this.password
      }

      Api.Login(params).then(res => {
        this.Token(res.data)
        this.GetUserInfo()
        this.$router.push({name: 'Personal'})
      }).catch(() => {
      })
    },
    handwxLogin() {
      this.$router.push({name: 'WechatLogin'})
    }
  },
  destroyed() {
    clearInterval(this.timer)
  }
}
</script>

<style lang="less" scoped>
main{
  padding: 0 20px;
}

.logo{
  width: 100px;
  margin: 20px auto;
  img{
    width: 100%;
    height: 100px;
    object-fit: cover;
    border-radius: 50%;
  }
}
.login-input{
  background: #f5f5f5;
  border-radius: 100px;
  padding: 12px 25px;
  margin-bottom: 12px;
}
.type-link{
  font-size: 14px;
  padding-bottom: 40px;
  display: flex;
  justify-content: space-between;
  color: #FF5722;
  a{
    color: #FF5722;
  }
}
.send-sms{
  color: #FF5722;
  &.disabled{
    color: #bbb;
  }
}
.hr{
  margin: 30px 0;
}
  /* .heading {
    text-align: center;
    margin-bottom: 48px;
    h1 {
      color: #333;
      line-height: 1.15;
      font-size: 64px;
      margin-bottom: 32px;
      span {
        position: relative;
      }
    }
  } */
  .heading h1{
    font-size: 20px;
  }
  .heading h1 span::after {
    content: "";
    position: absolute;
    z-index: -1;
    left: 0;
    bottom: 8px;
    width: 100%;
    height: 20px;
    background: #ecf6ff;
  }
  .heading p {
    font-size: 16px;
    color: #666;
    line-height: 56px;
  }
</style>
